package com.android.task4;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;

import android.os.RemoteException;
import android.util.Log;
import android.view.View;

public class MapView extends View {

	private final static int CACHE_SIZE = 50;
	private final static int TILE_SIZE = 256;
	private Bitmap tile;
	private Cache cache;
	
	Paint myPaint;
	
	private int x;
	private int y;
	private int zoom;
	
	public MapView(Context context) {
		super(context);
		cache = new Cache(CACHE_SIZE);
		myPaint = new Paint();
		this.myPaint.setColor(Color.RED);
	}//
	
	@Override
	public void onDraw(final Canvas c){

		createComposedView(c);

	}

	
	public void createComposedView( Canvas c){
		
		Bitmap cTile = cache.getBitmap(zoom, x, y).getBitmap();
		
		if(cTile!=null){
		
		if(zoom>0){
		Bitmap lTile = cache.getBitmap(zoom, x-1, y).getBitmap();
		Bitmap rTile = cache.getBitmap(zoom, x+1, y).getBitmap();
		Bitmap dTile = cache.getBitmap(zoom, x, y+1).getBitmap();
		Bitmap uTile = cache.getBitmap(zoom, x, y-1).getBitmap();
		Bitmap luTile = cache.getBitmap(zoom, x-1, y-1).getBitmap();
		Bitmap ruTile = cache.getBitmap(zoom, x+1, y-1).getBitmap();
		Bitmap ldTile = cache.getBitmap(zoom, x-1, y+1).getBitmap();
		Bitmap rdTile = cache.getBitmap(zoom, x+1, y+1).getBitmap();
		c.drawBitmap(cTile,(getWidth()-TILE_SIZE)/2 -1  ,(getHeight()-TILE_SIZE)/2 -1 , myPaint);
		
		
		int sideMargin = (getWidth()-TILE_SIZE)/2;
		int upbotMargin = (getHeight()-TILE_SIZE)/2;
		
		int left = TILE_SIZE - (getWidth()-TILE_SIZE)/2 ;
		int top = 0;
		int right = TILE_SIZE -1;
		int bottom = TILE_SIZE -1;
		Rect lSrc = new Rect(left, top, right, bottom);
		Rect lDst = new Rect(0, upbotMargin-1, sideMargin -1,upbotMargin -1 + TILE_SIZE);
		c.drawBitmap(lTile,lSrc,lDst , myPaint);
		
		Rect rSrc = new Rect( 0,0, sideMargin -1, TILE_SIZE-1);
		Rect rDst = new Rect( sideMargin + TILE_SIZE-1, upbotMargin-1, getWidth()-1, upbotMargin -1 + TILE_SIZE );
		c.drawBitmap(rTile, rSrc, rDst, myPaint);
		
		Rect uSrc = new Rect( 0,TILE_SIZE - upbotMargin-1 ,TILE_SIZE-1, TILE_SIZE-1);
		Rect uDst = new Rect( sideMargin -1, 0, sideMargin-1+TILE_SIZE, upbotMargin -1  );
		c.drawBitmap(uTile, uSrc, uDst, myPaint);
		
		Rect dSrc = new Rect(0, 0, TILE_SIZE-1, upbotMargin -1);
		Rect dDst = new Rect(sideMargin-1 ,upbotMargin  +TILE_SIZE -1,  sideMargin -1+TILE_SIZE, getHeight()-1);
		c.drawBitmap(dTile, dSrc, dDst, myPaint);
		
		Rect luSrc = new Rect(TILE_SIZE - sideMargin-1, TILE_SIZE -upbotMargin-1,TILE_SIZE -1, TILE_SIZE-1);
		Rect luDst = new Rect( 0, 0,sideMargin -1, upbotMargin-1 );
		c.drawBitmap(luTile, luSrc, luDst, myPaint);
		
		Rect ruSrc = new Rect(0, TILE_SIZE -upbotMargin-1, sideMargin -1, TILE_SIZE-1 );
		Rect ruDst = new Rect(TILE_SIZE+sideMargin -1, 0,  getWidth()-1, upbotMargin-1);
		c.drawBitmap(ruTile, ruSrc, ruDst, myPaint);
		
		Rect ldSrc = new Rect(TILE_SIZE-sideMargin-1,0 ,TILE_SIZE-1, upbotMargin-1);
		Rect ldDst = new Rect(0, TILE_SIZE+ upbotMargin-1, sideMargin-1, getHeight()-1);
		c.drawBitmap(ldTile, ldSrc, ldDst, myPaint);
		
		Rect rdSrc = new Rect(0, 0, sideMargin-1, upbotMargin-1);
		Rect rdDst = new Rect(TILE_SIZE+ sideMargin-1, TILE_SIZE+upbotMargin-1, getWidth()-1, getHeight()-1);
		c.drawBitmap(rdTile, rdSrc, rdDst, myPaint);
			
		}
		else {
			c.drawBitmap(cTile,(getWidth()-TILE_SIZE)/2 -1  ,(getHeight()-TILE_SIZE)/2 -1 , myPaint);
		}
		}
		else{
			c.drawText("No service", getWidth()/2-20, getHeight()/2, myPaint);
			
		}

	}
	
	
	
	public void getTile(int x, int y, int zoom){
		
		Log.i("INFO MapView","getTile, x: "+x+" y: "+y+" zoom: "+zoom);
		this.x = x;
		this.y = y;
		this.zoom = zoom;
		//tile = cache.getBitmap(zoom, x, y).getBitmap();
		//createComposedView(x,y,zoom);
		invalidate();
	}
	
}

